package com.fun.feifun.modules.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fun.feifun.modules.business.model.BusinessGoods;
import com.fun.feifun.modules.business.model.BusinessGoodsSupplierSort;
import com.fun.feifun.modules.business.dao.BusinessGoodsSupplierSortMapper;
import com.fun.feifun.modules.business.model.BusinessSupplier;
import com.fun.feifun.modules.business.model.BusinessUserGoods;
import com.fun.feifun.modules.business.model.dto.BusinessGoodsRouteDto;
import com.fun.feifun.modules.business.model.param.BusinessGoodsRoute;
import com.fun.feifun.modules.business.model.param.BusinessUserGoodsParam;
import com.fun.feifun.modules.business.service.IBusinessGoodsService;
import com.fun.feifun.modules.business.service.IBusinessGoodsSupplierSortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fun.feifun.modules.business.service.IBusinessSupplierGoodsService;
import com.fun.feifun.modules.business.service.IBusinessSupplierService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务商品路由配置 服务实现类
 * </p>
 *
 * @author wkf
 * @since 2024-06-20
 */
@Service
public class BusinessGoodsSupplierSortServiceImpl extends ServiceImpl<BusinessGoodsSupplierSortMapper, BusinessGoodsSupplierSort> implements IBusinessGoodsSupplierSortService {

    @Resource
    private IBusinessSupplierService supplierService;

    @Resource
    private IBusinessGoodsService goodsService;
    @Override
    public Boolean setGoodsRoute(BusinessGoodsRoute param) {
        List<BusinessGoodsSupplierSort> list=new ArrayList<>();
        //删除
        QueryWrapper<BusinessGoodsSupplierSort> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().in(BusinessGoodsSupplierSort::getGoodsId,param.getGoodsIds());
        if(!CollectionUtils.isEmpty(param.getSupplierIds())){
            queryWrapper.lambda().in(BusinessGoodsSupplierSort::getSupplierId,param.getSupplierIds());
            remove(queryWrapper);
        }else {
            remove(queryWrapper);
            return true;
        }
//        list(queryWrapper).forEach(it->{reMap.put(it.getGoodsId()+"||"+it.getSupplierId(),it);});
        //组装商品用户关联对象。
        for (Long goodsId:param.getGoodsIds()) {
            for (int i=0;i<param.getSupplierIds().size();i++) {
                BusinessGoodsSupplierSort sort=new BusinessGoodsSupplierSort();
                sort.setGoodsId(goodsId);
                sort.setSupplierId(param.getSupplierIds().get(i));
                sort.setPriorityLevel(i);
                list.add(sort);
            }
        }
        //插入
        boolean b = this.saveOrUpdateBatch(list);
        return b;
    }

    @Override
    public List<BusinessGoodsRouteDto> getGoodsRoute(Long goodsId) {
        return  this.baseMapper.getGoodsRoute(goodsId);
    }

    @Override
    public List<BusinessGoodsRouteDto> getGoodsRoutes(List<Long> goodsIds) {
        return  this.baseMapper.getGoodsRoutes(goodsIds);
    }

    @Override
    public BusinessGoodsRouteDto getRoute(Long goodsId,Long supplierId) {
        return  this.baseMapper.getRoute(goodsId,supplierId);
    }
}
